<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" class="left-icon" name="cross" @click="$router.go(-1)" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="userFormRef">
      <van-field v-model="mobile" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile" type="number"
        maxlength="11">
        <!-- 具名插槽 -->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="code" name="code" placeholder="请输入验证码" :rules="userFormRules.code" type="number"
        maxlength="6">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down @finish="showCountDown = false" v-if="showCountDown" :time="6*1000" format=" ss 秒" />
          <van-button v-else native-type="button" class="send-sms-btn" size="small" type="primary" @click="clickCode">
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="login-btn">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user.js'
export default {
  data () {
    return {
      mobile: '',
      code: '',
      userFormRules: {
        mobile: [
          { require: true, message: '请输入手机号' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]|19[0-9])[0-9]{8}$/, message: '手机号格式不正确' }],
        code: [
          { require: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' }
        ]
      },
      showCountDown: false
    }
  },
  methods: {
    // 提交表单且验证通过后触发
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true
      })
      // 利用try catch进行错误捕捉,但不会阻止代码往下进行
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        console.log(res)
        // 把获取的token储存在vuex中
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.go(-1)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('亲，账号或用户名错误')
        }
        this.$toast.fail('登录异常,登录失败')
      }
    },
    // 获取验证码
    async clickCode () {
      try {
        await this.$refs.userFormRef.validate('mobile')
      } catch (err) {
        return
      }
      console.log('校验成功')

      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true
      })
      // 发送验证码
      try {
        await sendCode(this.mobile)
        // 倒计时显示
        this.showCountDown = true
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('重复操作')
        }
        this.$toast.fail('系统错误')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 156px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
  border-radius: 40px;
}
.login-btn {
  margin: 53px 28px;
  border: none;
  .van-button {
    background-color: #6db4fb;
    border: none;
    border-radius: 10px;
  }
}
</style>
